/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#include "fr_local.h"

/*-----------------------------------------------------------------------------
	shading system :
-----------------------------------------------------------------------------*/


/*-----------------------------------------------------------------------------
	shader :
-----------------------------------------------------------------------------*/

//
//	EFRShader::EFRShader
//
EFRShader::EFRShader( EFRScene *scene, EString name, lua_State *L, int idx )
{
	effect				=	NULL;
	this->scene			=	scene;
	this->name			=	name;

	//	init parameters :	
	injection			=	"";
	
	texture_path0		=	"*black";
	texture_path1		=	"*black";
	texture_path2		=	"*black";
	texture_path3		=	"*black";
	texture_path4		=	"*black";
	texture_path5		=	"*black";

	is_solid			=	false;
	is_translucent		=	false;
	is_distortive		=	false;
	is_emissive			=	false;
	is_masked			=	false;

	resources.loaded	=	false;

	UpdateShader( L, idx );	
}


//
//	EFRShader::UpdateShader
//
void EFRShader::UpdateShader( lua_State *L, int idx )
{
	//	read parameters :	
	LuaGetField( L, idx, "texture_path0"	,	texture_path0		);
	LuaGetField( L, idx, "texture_path1"	,	texture_path1		);
	LuaGetField( L, idx, "texture_path2"	,	texture_path2		);
	LuaGetField( L, idx, "texture_path3"	,	texture_path3		);
	LuaGetField( L, idx, "texture_path4"	,	texture_path4		);
	LuaGetField( L, idx, "texture_path5"	,	texture_path5		);
	
	LuaGetField( L, idx, "injection"		,	injection			);

	LuaGetField( L, idx, "is_solid"			,	is_solid			);
	LuaGetField( L, idx, "is_translucent"	,	is_translucent		);
	LuaGetField( L, idx, "is_distortive"	,	is_distortive		);
	LuaGetField( L, idx, "is_emissive"		,	is_emissive			);
	LuaGetField( L, idx, "is_masked"		,	is_masked			);

	//	load fx file :
	LoadFXFile();

	//	cause texture reloading :
	resources.loaded	=	false;
}


//
//	EFRShader::~EFRShader
//
EFRShader::~EFRShader( void )
{
	effect	=	NULL;
}


//
//	EFRShader::LoadResources
//
void EFRShader::LoadResources( void )
{
	if (resources.loaded) {
		return;
	}

	resources.texture0	=	scene->rs->Driver()->GetTXM()->RegisterTexture( texture_path0 );
	resources.texture1	=	scene->rs->Driver()->GetTXM()->RegisterTexture( texture_path1 );
	resources.texture2	=	scene->rs->Driver()->GetTXM()->RegisterTexture( texture_path2 );
	resources.texture3	=	scene->rs->Driver()->GetTXM()->RegisterTexture( texture_path3 );
	resources.texture4	=	scene->rs->Driver()->GetTXM()->RegisterTexture( texture_path4 );
	resources.texture5	=	scene->rs->Driver()->GetTXM()->RegisterTexture( texture_path5 );

	resources.loaded	=	true;
}


//
//	EFRShader::LoadFXFile
//
void EFRShader::LoadFXFile( void )
{
	effect = scene->sshs->GetEffect( "shaders/surface.cgfx", injection );
	
	if (!effect) {
		return;
	}
	
	//
	//	get uniform handles :
	//																					
	uniforms.is_solid				=	effect->GetParameterByName( NULL, "is_solid"			);
	uniforms.is_translucent			=	effect->GetParameterByName( NULL, "is_translucent"		);
	uniforms.is_distortive			=	effect->GetParameterByName( NULL, "is_distortive"		);
	uniforms.is_emissive			=	effect->GetParameterByName( NULL, "is_emissive"			);
	uniforms.is_masked				=	effect->GetParameterByName( NULL, "is_masked"			);
																		   
	uniforms.is_skinned				=	effect->GetParameterByName( NULL, "is_skinned"			);
	uniforms.joints					=	effect->GetParameterByName( NULL, "joints"				);

	uniforms.random					=	effect->GetParameterByName( NULL, "random"				);
	uniforms.time					=	effect->GetParameterByName( NULL, "time"				);
																		   
	uniforms.entity_time			=	effect->GetParameterByName( NULL, "entity_time"			);		
	uniforms.entity_color			=	effect->GetParameterByName( NULL, "entity_color"		);
																		   					
	uniforms.matrix_world			=	effect->GetParameterByName( NULL, "matrix_world"		);
	uniforms.matrix_view			=	effect->GetParameterByName( NULL, "matrix_view"			);
	uniforms.matrix_proj			=	effect->GetParameterByName( NULL, "matrix_proj"			);
	
	uniforms.view_position			=	effect->GetParameterByName( NULL, "view_position"		);
																		   
	uniforms.texture0				=	effect->GetParameterByName( NULL, "sampler0"			 );
	uniforms.texture1				=	effect->GetParameterByName( NULL, "sampler1"			 );
	uniforms.texture2				=	effect->GetParameterByName( NULL, "sampler2"			 );
	uniforms.texture3				=	effect->GetParameterByName( NULL, "sampler3"			 );
	uniforms.texture4				=	effect->GetParameterByName( NULL, "sampler4"			 );
	uniforms.texture5				=	effect->GetParameterByName( NULL, "sampler5"			 );
	
	//
	//	get tech handles :
	//
	techniques.tech_fr_solid		=	effect->GetTechniqueByName	("tech_fr_solid");
	techniques.tech_fr_shadows		=	effect->GetTechniqueByName	("tech_fr_shadows");
	techniques.tech_fr_emissive		=	effect->GetTechniqueByName	("tech_fr_emissive");
	techniques.tech_fr_translucent	=	effect->GetTechniqueByName	("tech_fr_translucent");
	techniques.tech_fr_distortive	=	effect->GetTechniqueByName	("tech_fr_distortive");
	techniques.tech_fr_wireframe	=	effect->GetTechniqueByName	("tech_fr_wireframe");
}


//
//	EFRShader::BeginShading
//
bool EFRShader::BeginShading( const EFREntity *entity, uint shading_group_id )
{
	if (!effect) return false;
	
	EFRTech	tech	=	scene->sshs->GetTechnique();
	bool		skip	=	true;
	
	if ( tech == FR_TECH_SOLID			&&	is_solid		)	skip	=	false;
	if ( tech == FR_TECH_SHADOWS		&&	is_solid		)	skip	=	false;
	if ( tech == FR_TECH_EMISSIVE		&&	is_emissive		)	skip	=	false;
	if ( tech == FR_TECH_TRANSLUCENT	&&	is_translucent	)	skip	=	false;
	if ( tech == FR_TECH_DISTORTIVE		&&	is_distortive	)	skip	=	false;
	if ( tech == FR_TECH_WIREFRAME							)	skip	=	false;
	
	if (skip) {
		return false;
	}
	
	if ( tech == FR_TECH_SOLID			)	effect->SetTechnique(	techniques.tech_fr_solid		); else
	if ( tech == FR_TECH_SHADOWS		)	effect->SetTechnique(	techniques.tech_fr_shadows		); else
	if ( tech == FR_TECH_EMISSIVE		)	effect->SetTechnique(	techniques.tech_fr_emissive		); else
	if ( tech == FR_TECH_TRANSLUCENT	)	effect->SetTechnique(	techniques.tech_fr_translucent	); else
	if ( tech == FR_TECH_DISTORTIVE		)	effect->SetTechnique(	techniques.tech_fr_distortive	); else
	if ( tech == FR_TECH_WIREFRAME		)	effect->SetTechnique(	techniques.tech_fr_wireframe	);
	
	
	//SetUniform	(	uniforms.is_solid				,	is_solid					);
	//SetUniform	(	uniforms.is_translucent			,	is_translucent				);
	//SetUniform	(	uniforms.is_distortive			,	is_distortive				);
	//SetUniform	(	uniforms.is_emissive			,	is_emissive					);
	SetUniform	(	uniforms.is_masked				,	is_masked					);
													
	SetUniform	(	uniforms.random					,	scene->common.random		);
	SetUniform	(	uniforms.time					,	scene->common.time			);
													
	SetUniform	(	uniforms.entity_time			,	entity->time				);
	SetUniform	(	uniforms.entity_color			,	entity->color				);
																				
	SetUniform	(	uniforms.matrix_world			,	entity->GetWorldMatrix()	);
	SetUniform	(	uniforms.matrix_view			,	scene->view.matrix_view		);
	SetUniform	(	uniforms.matrix_proj			,	scene->proj.matrix_proj		);
	
	SetUniform	(	uniforms.view_position			,	scene->view.position		);
													
	if (shading_group_id==0) {
		if (entity->IsSkinned()) {
		
			SetUniform( uniforms.is_skinned, true );
			effect->SetUniformArray( uniforms.joints, entity->GetJointXFormArray(), GE_MAX_JOINT_NUM );
			
		} else {
			SetUniform( uniforms.is_skinned, false );
		}
	}
	
	//	textures :
	LoadResources();
	SetUniform( uniforms.texture0, resources.texture0 );
	SetUniform( uniforms.texture1, resources.texture1 );
	SetUniform( uniforms.texture2, resources.texture2 );
	SetUniform( uniforms.texture3, resources.texture3 );
	SetUniform( uniforms.texture4, resources.texture4 );
	SetUniform( uniforms.texture5, resources.texture5 );
	
	uint n;	
	effect->Begin(n);
	if (n==0) {
		effect->End();
		return false;
	}
	
	effect->BeginPass(0);
	
	return true;
}

//
//	EFRShader::EndShading
//
void EFRShader::EndShading( void )
{
	if (!effect) return;

	effect->EndPass();
	effect->End();
}


/*-----------------------------------------------------------------------------
	uniform helpers :
-----------------------------------------------------------------------------*/

void EFRShader::SetUniform( EFXHandle handle, float v ) {
	effect->SetUniform( handle, v );
}

void EFRShader::SetUniform( EFXHandle handle, const EVector v ) {
	effect->SetUniform( handle, v );
}

void EFRShader::SetUniform( EFXHandle handle, const EPoint p ) {
	effect->SetUniform( handle, p );
}

void EFRShader::SetUniform( EFXHandle handle, const EQuaternion q ) {
	effect->SetUniform( handle, q );
}

void EFRShader::SetUniform( EFXHandle handle, const EColor c ) {
	effect->SetUniform( handle, c );
}

void EFRShader::SetUniform( EFXHandle handle, const EMatrix m ) {
	effect->SetUniform( handle, m );
}

void EFRShader::SetUniform( EFXHandle handle, bool b ) {
	effect->SetUniform( handle, b );
}

void EFRShader::SetUniform( EFXHandle handle, IPxTexture texture ) {
	effect->SetTexture( handle, texture );
}

void EFRShader::CommitChanges( void ) {
	effect->CommitChanges();
}
